define(["jquery","comm/form/form.comm"],function($,$comm){
	var quest = {};

	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	if( obj.list && $.isArray(obj.list) && obj.list.length > 0 ){
    		for(var i = 0; i < obj.list.length; i++){
    			if( typeof obj.list[i] == "string" ){
    				var str = obj.list[i];
    				obj.list[i] = {};
    				obj.list[i].key = str;
    				obj.list[i].text = str;
    			}
    			if( obj.relation ){
    				for (var j = 0; j < obj.relation.length; j++) {
			            if( obj.relation[j].relationKey == obj.list[i].key){
			                obj.list[i].relationId = obj.relation[j].relationId;
			                break;
			            }
			        } 
    			} 
    		}
    	}
		return obj;
	}

	/**
	 * tpl模板 (data-abnormal为了母子健康手册异常label获取)
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_radioRelation" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} {{if getLabelRelationId}}data-getLabelRelationId="{{getLabelRelationId}}"{{/if}} class="qaCard qaCard-radioRelation form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'		{{if status=="default"}}'+
				'   		<div class="control-input radio">'+
				'				{{each list as item i}}'+
				'					<label class="radio-inline">'+
				'						<input type="radio" class="validatebox" name="{{name}}" rId="{{item.relationId}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" {{if dataUnit}}data-dataUnit="{{dataUnit}}"{{/if}} {{if item.abnormal}}data-abnormal="{{item.abnormal}}"{{/if}} data-validate="{{validate}}" data-attention="{{attention}}">'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{else if status=="readonly"}}'+	
				'   		<div class="control-input radio disabled">'+
				'				{{each list as item i}}'+
				'					<label class="radio-inline">'+
				'						<input type="radio" class="validatebox" name="{{name}}" rId="{{item.relationId}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" {{if dataUnit}}data-dataUnit="{{dataUnit}}"{{/if}} {{if item.abnormal}}data-abnormal="{{item.abnormal}}"{{/if}} data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{else if status=="disabled"}}'+
				'   		<div class="control-input radio disabled">'+
				'				{{each list as item i}}'+
				'					<label class="radio-inline">'+
				'						<input type="radio" class="validatebox" name="{{name}}" rId="{{item.relationId}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" {{if dataUnit}}data-dataUnit="{{dataUnit}}"{{/if}} {{if item.abnormal}}data-abnormal="{{item.abnormal}}"{{/if}} data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{/if}}'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			$dom.each(function(i){
				if( value.quesValue == $(this).val() ){
					$(this).attr("checked","checked");
					$(this).trigger("click");
					return true; // 退出本次循环
				}
			});
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var dom = $dom.parents(".qaCard").find("input[type='radio'][name='" + name + "']:checked");
		var value = {};
		if( dom.length > 0 ){
			value.quesValue = dom.val();
		}
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}

	/**
	 * 事件函数
	 */
	quest.event = function(eventParams,id){
		// 单选框可点击取消选中
		$comm.cancelRadioChecked();
		// 选中某个单选项，某个单选项对应的才显示
		var dom = $("#" + id + " .qaCard[relationId]"); // 表单里带relationId的dom元素
		// 初始化
		dom.each(function(i){
			dom.eq(i).hide();
		});
		$("#" + id + " .qaCard-radioRelation .radio input:radio").unbind("click");
		$("#" + id + " .qaCard-radioRelation .radio input:radio").data({"dom":dom});
		$("#" + id + " .qaCard-radioRelation .radio input:radio").bind("click",onRadioRelation);
	}

	var onRadioRelation = function(event){
		var dom = $(this).data("dom");
		var This = $(this);
		var rId = This.attr("rId");
		dom.each(function(j){
			if(dom.eq(j).attr("relationId") == rId){
				// event.target.previousValu 取消单选框选中状态时设置的
				if( event.target.previousValue ){
				 	dom.eq(j).hide();
				 	domHideHasClass(dom,j,"hide");
				}else{
				 	dom.eq(j).show();
				 	if( dom.eq(j).hasClass("qaCard-radioRelation") ){
				 		dom.eq(j).find(".radio input:radio:checked").each(function(){
				 			if( $(this).attr("rId") != "" ){
				 				var rId1 = $(this).attr("rId");
								dom.each(function(p){
									if(dom.eq(p).attr("relationId") == rId1){
										dom.eq(p).show();
									}
								});
				 			}
						});
				 	}
				}
			}else{
				This.parents(".qaCard-radioRelation").find(".radio input:radio").each(function(){
					if( dom.eq(j).attr("relationId") == $(this).attr("rId") ){
					 	dom.eq(j).hide();
					 	domHideHasClass(dom,j,"hide");
					}
				});
			}
		});
	}

	/**
	 * 勾选单选框决定是否显示相关联的题目
	 */
	var domHideHasClass = function(dom,j,flag){
		if( dom.eq(j).hasClass("qaCard-radioRelation") ){
	 		dom.eq(j).find(".radio input:radio").each(function(){
	 			if( $(this).attr("rId") != "" ){
	 				var rId1 = $(this).attr("rId");
					dom.each(function(p){
						if(dom.eq(p).attr("relationId") == rId1){
							if( flag == "show" ){
								dom.eq(p).show();
							}else if( flag == "hide" ){
								dom.eq(p).hide();
							}
						}
					});
	 			}
			});
	 	}
	}
    
	return quest;
});